package com.qf.flash.mvp.presenter;

import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.http.HttpManager;
import com.qf.flash.http.response.BusinessInfoBean;
import com.qf.flash.model.CommentBean;
import com.qf.flash.mvp.contract.BusinessDetContract;
import com.qf.flashlib.base.BasePresenter;
import com.qf.flashlib.base.BaseResponse;
import com.qf.flashlib.base.BaseSubscriber;
import com.qf.flashlib.util.RxUtils;

import java.util.List;

/**
 * 描述:商家详情
 * 2020/3/27 15:24
 */
public class BDPresenter extends BasePresenter<BusinessDetContract.View> implements BusinessDetContract.Presenter {

    @Override
    public void getBusinessDetail(String shopId) {
        String uid = UserMgr.getInstance().getLoginUser().getUid();
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .BUSINESS_DETAIL(shopId, uid)
                .compose(RxUtils.handleResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<BusinessInfoBean>(mRootView) {
                    @Override
                    public void onSuccess(BusinessInfoBean response) {
                        mRootView.setBusinessData(response);
                    }
                })
        );
    }

    @Override
    public void collectShop(String shopId) {
        String uid = UserMgr.getInstance().getLoginUser().getUid();
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .INSERT_COLLECT_SHOP(uid,shopId)
                .compose(RxUtils.handleResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<Object>(mRootView) {
                    @Override
                    public void onSuccess(Object response) {
//                        mRootView.setBusinessData(response);
                        mRootView.setCollected();
                    }
                })
        );
    }

    @Override
    public void deleteCollectShop(String shopIds) {
        String uid = UserMgr.getInstance().getLoginUser().getUid();
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .DELETE_COLLECT_SHOP(uid, shopIds)
                .compose(RxUtils.handleBaseResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<BaseResponse<Object>>(mRootView) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mRootView.cancelCollectSuccess();
                    }
                })
        );
    }

    @Override
    public void getResponseList(String shopId, int pageNo) {
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .BUSINESS_RESPONSE_LIST(shopId, pageNo)
                .compose(RxUtils.handleBaseResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<BaseResponse<List<CommentBean>>>(mRootView) {
                    @Override
                    public void onSuccess(BaseResponse<List<CommentBean>> response) {
                        mRootView.setResponseList(response.getData(), response.getTotalPage());
                    }

                })
        );
    }
}
